//
//  MyViewController.m
//  test1
//
//  Created by 衣尚品1 on 15/5/6.
//  Copyright (c) 2015年 衣尚品1. All rights reserved.
//

#import "MyViewController.h"

@interface MyViewController (){
    UIImageView *tabbarImage;
    BOOL isEdit;
}

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.select1.layer.borderWidth = 1;
    self.select2.layer.borderWidth = 1;
    self.select3.layer.borderWidth = 1;
    self.select4.layer.borderWidth = 1;
    self.select5.layer.borderWidth = 1;
    self.select6.layer.borderWidth = 1;
    //    self.hideView1.hidden = YES;
    self.hideView2.hidden = YES;
    self.hideView3.hidden = YES;
    self.button1.layer.borderWidth = 2;
    self.button2.layer.borderWidth = 2;
    self.button3.layer.borderWidth = 2;
    self.viewHeight.constant = 160;
    
//    tabbarImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, self.demandScrollView.frame.origin.y + 15, 15, 15)];
    tabbarImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2, self.demandScrollView.frame.origin.y - 15, 15, 15)];
    tabbarImage.image = [UIImage imageNamed:@"MHTabBarIndicator.png"];
    [self.view insertSubview:tabbarImage atIndex:3];
    
    self.demandScrollView.hidden = YES;

    _name.enabled = NO;
    _sex.enabled = NO;
    _age.enabled = NO;
    _phoneNum.enabled = NO;
    _address.enabled = NO;
    isEdit = NO;
}

- (IBAction)click:(id)sender {
    self.hideView2.hidden = YES;
    self.hideView3.hidden = YES;
    if (self.viewHeight.constant == 160) {
        self.viewHeight.constant = 40;
        [UIView transitionWithView:self.hideView1 duration:0.3 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
            self.hideView1.hidden = YES;
        } completion:nil];
        
    }
    else {
        self.viewHeight.constant = 160;
        self.view2Height.constant = 40;
        self.view3Height.constant = 40;
        [UIView transitionWithView:self.hideView1 duration:0.5 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
            self.hideView1.hidden = NO;
        } completion:nil];
    }
    [UIView animateWithDuration:0.3 animations:^{
        [self.view layoutIfNeeded];
        
    }];
    
    
    
}
- (IBAction)click2:(id)sender {
    self.hideView1.hidden = YES;
    self.hideView3.hidden = YES;
    if (self.view2Height.constant == 270) {
        self.view2Height.constant = 40;
        [UIView transitionWithView:self.hideView2 duration:0.3 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
            self.hideView2.hidden = YES;
        } completion:nil];
        
    }
    else {
        self.view2Height.constant = 270;
        self.viewHeight.constant = 40;
        self.view3Height.constant = 40;
        [UIView transitionWithView:self.hideView2 duration:0.5 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
            self.hideView2.hidden = NO;
        } completion:nil];
    }
    [UIView animateWithDuration:0.3 animations:^{
        [self.view layoutIfNeeded];
        
    }];
}
- (IBAction)clickSelect1:(id)sender {
    if (self.select1.selected) {
        NSLog(@"no");
        self.select1.selected = NO;
    }
    else{
        NSLog(@"yes");
        self.select1.selected = YES;
    }
}
- (IBAction)clickSelect2:(id)sender {
    if (self.select2.selected) {
        NSLog(@"no");
        self.select2.selected = NO;
    }
    else{
        NSLog(@"yes");
        self.select2.selected = YES;
    }
}
- (IBAction)clickSelect3:(id)sender {
    if (self.select3.selected) {
        NSLog(@"no");
        self.select3.selected = NO;
    }
    else{
        NSLog(@"yes");
        self.select3.selected = YES;
    }
}
- (IBAction)cilckSelect4:(id)sender {
    if (self.select4.selected) {
        NSLog(@"no");
        self.select4.selected = NO;
    }
    else{
        NSLog(@"yes");
        self.select4.selected = YES;
    }
}
- (IBAction)clickSelect5:(id)sender {
    if (self.select5.selected) {
        NSLog(@"no");
        self.select5.selected = NO;
    }
    else{
        NSLog(@"yes");
        self.select5.selected = YES;
    }
}
- (IBAction)clickSelect6:(id)sender {
    if (self.select6.selected) {
        NSLog(@"no");
        self.select6.selected = NO;
    }
    else{
        NSLog(@"yes");
        self.select6.selected = YES;
    }
}
- (IBAction)click3:(id)sender {
    self.hideView1.hidden = YES;
    self.hideView2.hidden = YES;
    if (self.view3Height.constant == 260) {
        self.view3Height.constant = 40;
        [UIView transitionWithView:self.hideView3 duration:0.3 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
            self.hideView3.hidden = YES;
        } completion:nil];
        
    }
    else {
        self.view3Height.constant = 260;
        self.viewHeight.constant = 40;
        self.view2Height.constant = 40;
        [UIView transitionWithView:self.hideView3 duration:0.5 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
            self.hideView3.hidden = NO;
        } completion:nil];
    }
    [UIView animateWithDuration:0.3 animations:^{
        [self.view layoutIfNeeded];
        
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)clickUserInfo:(id)sender {
    [UIView animateWithDuration:0.15 animations:^{
        [tabbarImage setTransform:(CGAffineTransformMakeTranslation(0, 0))];
    }];
    self.demandScrollView.hidden = YES;
    self.userInfoView.hidden = NO;
}

- (IBAction)clickUserDemand:(id)sender {
    [UIView animateWithDuration:0.15 animations:^{
        [tabbarImage setTransform:(CGAffineTransformMakeTranslation(100, 0))];
                                   }];
    self.demandScrollView.hidden = NO;
    self.userInfoView.hidden = YES;
}
- (IBAction)clickEditButton:(id)sender {
    if (isEdit) {
        [self.editButton setTitle:@"编辑" forState:0];
        isEdit = NO;
        [self.view becomeFirstResponder];
        _name.enabled = NO;
        _sex.enabled = NO;
        _age.enabled = NO;
        _phoneNum.enabled = NO;
        _address.enabled = NO;
    }
    else {
        [self.editButton setTitle:@"保存" forState:0];
        isEdit = YES;
        _name.enabled = YES;
        _sex.enabled = YES;
        _age.enabled = YES;
        _phoneNum.enabled = YES;
        _address.enabled = YES;
    }
    
}
- (IBAction)clickHeadPic:(id)sender {
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
    [actionSheet addButtonWithTitle:@"拍照"];
    [actionSheet addButtonWithTitle:@"从手机相册选择"];
    // 同时添加一个取消按钮
    [actionSheet addButtonWithTitle:@"取消"];
    // 将取消按钮的index设置成我们刚添加的那个按钮，这样在delegate中就可以知道是那个按钮
    actionSheet.destructiveButtonIndex = actionSheet.numberOfButtons - 1;
    [actionSheet showInView:self.view];
}

#pragma mark - 判断设备是否有摄像头

- (BOOL) isCameraAvailable
{
    return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
}


#pragma mark - 选择图片来源

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{
    
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.editing = YES;
    imagePicker.allowsEditing = YES;
    imagePicker.delegate = self;
    
    if (buttonIndex == 0)//照相机
    {
        if ([self isCameraAvailable]) {
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:imagePicker animated:YES completion:nil];
            //            [self presentModalViewController:imagePicker animated:YES];
        }else{
            
            UIAlertView *aler=[[UIAlertView alloc]initWithTitle:@"提示" message:@"该设备没有摄像头" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [aler show];
        }
    }
    if (buttonIndex == 1)
    {
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:imagePicker animated:YES completion:nil];
        //        [self presentModalViewController:imagePicker animated:YES];
    }
    
    
}

#pragma mark - UIImagePicker delegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    //    [picker dismissModalViewControllerAnimated:YES];
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    [self.HeadPic setImage:image forState:0];
    //    [self performSelector:@selector(saveImage:) withObject:image afterDelay:0.5];
}


@end
